home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / NSL 1.0 SDK / Sample Code / Test.c < prev   
Encoding:
C/C++ Source or Header  |  1998-10-05  |  8.8 KB  |  333 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Test.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Kevin Arnold - based on sample code from Nav services written by Tony Bacigalupi
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.      <5>     8/25/98    KA        we weren't setting done to false in the beginning of TestSyncServicesLookup
  13.      <4>     8/12/98    KA        added cleanup calls to free our NSL made objects
  14.      <3>     7/21/98    KA        added NSLErrorToString calls
  15.      <1>     4/06/98    KA        Initial check-in.
  16.  
  17.     To Do:
  18. */
  19.  
  20. #include "NSLAPI.h"
  21.  
  22.  
  23. #if PROFILE
  24. #include <Profiler.h>
  25. #endif
  26.  
  27. #include <StdIO.h>
  28. #include <string.h>
  29.  
  30. #ifdef __MWERKS__
  31. #include <SIOUX.h>
  32. #endif
  33.  
  34.  
  35. #define kWebServerType        "http"
  36. #define kFTPServerType        "ftp"
  37. #define kAppleShareType        "AFPServer"
  38. #define kLaserWriterType    "LaserWriter"
  39.  
  40. NSLClientRef        gOurClientRef;
  41. char                gErrorString[256];
  42. char                gSolutionString[256];
  43.  
  44. void TestDefaultNeighborhoodLookup( void );
  45. void TestSyncNeighborhoodLookup( NSLNeighborhood neighborhood ); 
  46. void TestSyncServicesLookup( char* service );
  47.  
  48.  
  49. static char GetPressedKey()
  50. {
  51.     char c;
  52.     char dummy;
  53.     
  54.     scanf("%c%c", &c, &dummy);
  55.     
  56.     return c;
  57. }
  58.  
  59. void main()
  60. {
  61.     OSStatus    status;
  62.     Boolean     quit        = false;
  63.     Boolean        doBanner    = true;
  64.         
  65.     #ifdef __MWERKS__
  66.     SIOUXSettings.asktosaveonclose = 0;
  67.     #endif
  68.  
  69.     #if PROFILE
  70.     ProfilerInit(collectDetailed, bestTimeBase, 500, 20);
  71.     #endif
  72.  
  73.     printf("NSL API Test\rGenerated %s at %s\r\r", __DATE__, __TIME__);
  74.         
  75.     status = NSLOpenNavigationAPI( &gOurClientRef );
  76.     
  77.     if ( status && status != kNSLSomePluginsFailedToLoad )
  78.         printf("NSLAPI could not be opened due to an internal error\r");
  79.     else
  80.     {
  81.         if ( status == kNSLSomePluginsFailedToLoad )
  82.         {
  83.             // this isn't a fatal error, so we want to let someone know but we will continue.
  84.             NSLError    cludgeError;            // we have to crock one as this is the only time we will get back an OSStatus
  85.                                                 // instead of an NSLError.  Just make theContext be zero
  86.             cludgeError.theErr = status;
  87.             cludgeError.theContext = 0;
  88.             
  89.             NSLErrorToString( cludgeError, gErrorString, gSolutionString );
  90.             printf("Error #%ld: %s\r", cludgeError.theErr, gErrorString );
  91.             printf("Solution: %s\r", gSolutionString );
  92.         }
  93.         
  94.         do {
  95.             char c;
  96.             
  97.             if ( doBanner )
  98.                 printf("\r\r\rPlease make a choice [1-6 and hit return]:\r\t1 - Get list of Default Neighborhoods available\r\t2 - Get list of Webservers in a Neighborhood\r\t3 - Get list of ftp servers in a Neighborhood\r\t4 - Get list of AppleShare servers in a Neighborhood\r\t5 - Get list of LaserWriters in a Neighborhood\r\t6 - QUIT\r\r> ");
  99.  
  100.             c = GetPressedKey();        
  101.             
  102.             doBanner = true;
  103.             
  104.             switch ( c)
  105.             {
  106.                 case '1':
  107.                     TestDefaultNeighborhoodLookup();
  108.                     break;
  109.                     
  110.                 case '2':
  111.                     TestSyncServicesLookup(kWebServerType);
  112.                     break;
  113.                     
  114.                 case '3':
  115.                     TestSyncServicesLookup(kFTPServerType);
  116.                     break;
  117.                     
  118.                 case '4':
  119.                     TestSyncServicesLookup(kAppleShareType);
  120.                     break;
  121.                     
  122.                 case '5':
  123.                     TestSyncServicesLookup(kLaserWriterType);
  124.                     break;
  125.                     
  126.                 case '6':
  127.                     printf("Bye now!\r");
  128.                     quit = true;
  129.                     break;
  130.                 
  131.                 default:
  132.                     doBanner = false;
  133.             }
  134.  
  135.         } while ( quit == false );
  136.     }
  137.     
  138.     #if PROFILE
  139.     ProfilerDump("\pPerformances");
  140.     ProfilerTerm();
  141.     #endif
  142.     
  143.     ExitToShell();
  144. }
  145.  
  146. #define    kBufferLength    4096
  147.  
  148. void TestDefaultNeighborhoodLookup( void )
  149. {
  150.     NSLNeighborhood neighborhood;
  151.     
  152.     printf( "Getting default NSLNeighborhoods\r\r" );
  153.     
  154.     neighborhood = NSLMakeNewNeighborhood( "", NULL );        // empty string for default neighborhood lookup, NULL for empty protocol string 
  155.                                                             // meaning we want all default neighborhoods
  156.     TestSyncNeighborhoodLookup( neighborhood );
  157.  
  158.     NSLFreeNeighborhood( neighborhood );                    // make sure we free up this memory
  159. }
  160.  
  161.  
  162.  
  163. void TestSyncNeighborhoodLookup( NSLNeighborhood neighborhood )
  164. {
  165.     char                tempName[256];
  166.     long                bufLen = kBufferLength;
  167.     char*                buffer = NULL;
  168.     char*                tempPtr = NULL;
  169.     NSLRequestRef        ourRequestRef;
  170.     ClientAsyncInfoPtr    ourAsyncInfo;
  171.     NSLError            iErr = kNSLErrorNoErr;
  172.     NSLNeighborhood        nhPtr = NULL;
  173.     long                nhLength, tempPtrLength;
  174.     Boolean                done = false;
  175.     
  176.     printf( "Looking up Neighborhoods\r\r" );
  177.     
  178.     buffer = NewPtr( bufLen );
  179.     
  180.     // first prepare the request which will set up a ClientAsyncInfoPtr for us
  181.     iErr = NSLPrepareRequest( NULL, NULL, gOurClientRef, &ourRequestRef, buffer, bufLen, &ourAsyncInfo );
  182.  
  183.     if ( iErr.theErr )
  184.     {
  185.         printf("NSLPrepareRequest returned error %ld\r", iErr.theErr );
  186.         NSLErrorToString( iErr, gErrorString, gSolutionString );
  187.         printf("Error: %s\r", gErrorString );
  188.         printf("Solution: %s\r", gSolutionString );
  189.     }
  190.         
  191.     // set the values of ourAsyncInfo pb
  192.     ourAsyncInfo->clientContextPtr = NULL;
  193.     ourAsyncInfo->maxSearchTime = 0;        // no max search time
  194.     ourAsyncInfo->alertInterval = 0;         // no alert interval
  195.     ourAsyncInfo->alertThreshold = 1;        // make alert threshold every item...
  196.  
  197.     if ( iErr.theErr == noErr )
  198.         iErr = NSLStartNeighborhoodLookup( ourRequestRef, neighborhood, ourAsyncInfo );
  199.     
  200.     do {
  201.         if ( iErr.theErr == noErr && ourAsyncInfo->totalItems > 0 )
  202.         {
  203.             while ( NSLGetNextNeighborhood( ourAsyncInfo, &nhPtr, &nhLength ) )
  204.             {        
  205.                 if ( nhLength > 0 && nhLength < kBufferLength  )
  206.                 {
  207.                     NSLGetNameFromNeighborhood( nhPtr, &tempPtr, &tempPtrLength );
  208.                     memcpy( tempName, tempPtr, tempPtrLength );
  209.                     tempName[tempPtrLength] = '\0';
  210.  
  211.                     printf( "%s\r", tempName );
  212.                 }
  213.             }
  214.         }
  215.         
  216.         if ( ourAsyncInfo->searchState == kNSLSearchStateComplete )
  217.             done = true;
  218.         else if ( iErr.theErr == noErr )
  219.             iErr = NSLContinueLookup( ourAsyncInfo );
  220.         
  221.     } while ( !iErr.theErr && !done );
  222.     
  223.     if ( iErr.theErr )
  224.     {
  225.         NSLErrorToString( iErr, gErrorString, gSolutionString );
  226.         printf("Error: %s\r", gErrorString );
  227.         printf("Solution: %s\r", gSolutionString );
  228.     }    
  229.     
  230.     if ( buffer )
  231.         DisposePtr(buffer);
  232. }
  233.  
  234. void TestSyncServicesLookup( char* service )
  235. {
  236.     char                name[256] = "\p";
  237.     char                url[1024];
  238.     long                bufLen = kBufferLength;
  239.     char*                buffer = NewPtr( bufLen );
  240.     NSLRequestRef        ourRequestRef;
  241.     ClientAsyncInfoPtr    ourAsyncInfo;
  242.     NSLError            iErr = kNSLErrorNoErr;
  243.     NSLServicesList        serviceList = NULL;
  244.     TypedDataPtr         newDataPtr = NULL;
  245.     NSLNeighborhood        neighborhood, urlPtr = NULL;
  246.     long                urlLength;
  247.     Boolean                done = false;
  248.     char                c;
  249.     
  250.     printf( "Please Type in the neighborhood you want to search then hit return\r" );
  251.     scanf("%c", &c );
  252.     
  253.     while ( c != '\r' && c != '\n' && name[0] < 256 )
  254.     {
  255.         name[0]++;
  256.         name[name[0]] = c;
  257.         scanf("%c", &c );
  258.     };
  259.     
  260.     p2cstr( (unsigned char*)name );
  261.     
  262.     neighborhood = NSLMakeNewNeighborhood( name, NULL );
  263.     printf( "Looking up Service\r\r" );
  264.     
  265.     // first prepare the request which will set up a ClientAsyncInfoPtr for us
  266.     iErr = NSLPrepareRequest( NULL, NULL, gOurClientRef, &ourRequestRef, buffer, bufLen, &ourAsyncInfo );
  267.  
  268.     if ( iErr.theErr )
  269.     {
  270.         printf("NSLPrepareRequest returned error %ld\r", iErr.theErr );
  271.  
  272.         NSLErrorToString( iErr, gErrorString, gSolutionString );
  273.         printf("Error #%ld: %s\r", iErr.theErr, gErrorString );
  274.         printf("Solution: %s\r", gSolutionString );
  275.     }
  276.     else
  277.     {
  278.         serviceList = NSLMakeNewServicesList( service );    // we can pass a comma delimited cstring here (ie "http,ftp")
  279.         
  280.         // now we need to create a TypedDataPtr which holds teh serviceList info as well as an attribute if we wish
  281.         if ( serviceList != NULL )
  282.         {
  283.             iErr.theErr = NSLMakeRequestPB( serviceList, "", &newDataPtr );    // we can also pass an attribute if we want to get more specific
  284.             NSLDisposeServicesList( serviceList );                            // we are done with this, free this memory!        
  285.         }
  286.         
  287.         // set the values of ourAsyncInfo pb
  288.         ourAsyncInfo->clientContextPtr = NULL;
  289.         ourAsyncInfo->maxSearchTime = 0;        // no max search time
  290.         ourAsyncInfo->alertInterval = 0;         // no alert interval
  291.         ourAsyncInfo->alertThreshold = 1;        // make alert threshold every item...
  292.  
  293.         if ( iErr.theErr == noErr )
  294.             iErr = NSLStartServicesLookup( ourRequestRef, neighborhood, newDataPtr, ourAsyncInfo );
  295.  
  296.         do {
  297.             if ( iErr.theErr == noErr && ourAsyncInfo->totalItems > 0 )
  298.             {
  299.                 while ( NSLGetNextUrl( ourAsyncInfo, &urlPtr, &urlLength ) )
  300.                 {        
  301.                     if ( urlLength > 0 )
  302.                     {
  303.                         memcpy( url, urlPtr, urlLength );
  304.                         url[urlLength] = '\0';
  305.                         printf( "%s\r", url );
  306.                     }
  307.                 }
  308.             }
  309.             else if ( iErr.theErr )
  310.             {
  311.                 NSLErrorToString( iErr, gErrorString, gSolutionString );
  312.                 printf("Error #%ld: %s\r", iErr.theErr, gErrorString );
  313.                 printf("Solution: %s\r", gSolutionString );
  314.             }
  315.                     
  316.             if ( ourAsyncInfo->searchState == kNSLSearchStateComplete )
  317.                 done = true;
  318.             else if ( iErr.theErr == noErr )
  319.                 iErr = NSLContinueLookup( ourAsyncInfo );
  320.  
  321.         } while ( !done );                                    // it is possible to get errors back, but if the search state isn't complete, then one or more
  322.                                                             // plugins are still working!
  323.  
  324.         NSLFreeTypedDataPtr( newDataPtr );                    // dispose this
  325.     }
  326.     
  327.     NSLFreeNeighborhood( neighborhood );                    // make sure we free up this memory
  328.     
  329.     if ( buffer )
  330.         DisposePtr(buffer);
  331. }
  332.  
  333.